package per.kongtouxu.controller;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import per.kongtouxu.domain.dto.JsonDto;
import per.kongtouxu.domain.pojo.User;
import per.kongtouxu.domain.pojo.UserScoreIo;
import per.kongtouxu.service.Impl.RegInfoServiceImpl;
import per.kongtouxu.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RegInfoServiceImpl regInfoService;
    //用户登录
    @RequestMapping("/userLogin")
    public @ResponseBody JsonDto userLogin(HttpServletRequest req, HttpServletResponse resp)throws Exception{

        System.out.println("开始匹配验证码!!");
        //1.获取页面传来的检验码
        String check = req.getParameter("check");
        //2.从session中获取验证码
        HttpSession session = req.getSession();
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
        session.removeAttribute("CHECKCODE_SERVER");//为了保证验证码只能使用一次
        //2.2用于返回的信息
        JsonDto info = new JsonDto();
        //3.比较验证码
        if(checkcode_server == null || !checkcode_server.equalsIgnoreCase(check)) {
            System.out.println(check);
            System.out.println(checkcode_server);
            //验证码错误
            //1.注册失败
            info.setId(0);
            info.setMessage("验证码错误！！");
            //2.将info对象序列化为Json
            return info;
        }
        //1.获取用户名和密码数据
        Map<String, String[]> map = req.getParameterMap();
        //2.封装User对象
        User user = new User();
        try {
            BeanUtils.populate(user,map);
            System.out.println("自动封装之后的bean："+user);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //3.调用service查询
        User loginUser = userService.loginUser(user.getUserName(),user.getUserPwd());
        System.out.println("查询后的后台用户："+loginUser);
        //4.判断用户是否为空
        if(loginUser == null){
            //用户名密码错误
            info.setId(0);
            info.setMessage("用户名密码错误！！");
        } else {
            //用户名正确
            info.setId(1);
            info.setMessage("登录成功！！");
            info.setLocation("index");
            info.getDatas().put("user",loginUser);
            req.getSession().setAttribute("loginUser",loginUser);
        }
        //5.响应数据
        return info;
    }
    //用户注册
    @RequestMapping("/userRegister")
    public @ResponseBody JsonDto userRegister(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        //1.获取参数
        Map<String, String[]> map = req.getParameterMap();
        //2.封装User对象
        User user = new User();
        try {
            BeanUtils.populate(user,map);
            System.out.println("自动封装之后的bean："+user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //3.调用service注册用户
        //查询具体的静态变量,添加积分
        user.setUserScore(regInfoService.findRegInfoById(1).getRegScore());
        int result = userService.saveUser(user);
        //3.1返回的信息
        JsonDto info = new JsonDto();
        //4.result = 1 或0
        if(result == 0){
            //用户已经存在
            info.setId(0);
            info.setMessage("用户已经存在，请修改用户名！！");
        }else if(result == 1){
            //用户不存在，已经注册成功
            info.setId(1);
            info.setMessage("注册成功！！");
            info.setLocation("Login");


        }
        //5.响应数据
        return info;
    }
    //根据账户查询User
    @RequestMapping("/findUserByAccount")
    public @ResponseBody User findUserByAccount(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        User loginUser = (User) req.getSession().getAttribute("loginUser");
        User user = userService.findUserByAccount(loginUser.getUserName());
        System.out.println("user:"+user);
        return user;
    }
}
